package com.sdzee.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

public class MySQLAccessEvt {
	
  private Connection connect = null;
  private Statement statement = null;
  private PreparedStatement preparedStatement = null;
  private ResultSet resultSet = null;
  String url = "jdbc:mysql://82.224.100.119:3306/SMV";
  String utilisateur = "root";
  String motDePasse = "Smv2014";
  String[] strs = null;
  Date dateEvt = null;
  String titre = null;
  String description = null;
  String adresse = null;
  String categorie = null;
  String duree = null;
  int userID =0;
  int nbParticipantActuel=6;
  int nbParticipantMax=10;
  int note=4;
  int etat=1;
  String UrlPhotoEvenement=null;
    
  public void insertDataBase(String mess) throws Exception {
	  
	 strs = mess.split("/");	  
     titre = strs[0];;
     description = strs[2];;
     adresse = strs[1];;
     categorie = strs[3];;
     UrlPhotoEvenement="urlPhoto";
     

    try {
     
      Class.forName("com.mysql.jdbc.Driver");
      connect = DriverManager.getConnection( url, utilisateur, motDePasse );

     
      statement = connect.createStatement();   
      preparedStatement = connect.prepareStatement("insert into  SMV.Evenement values (default, ?,?,?,?,?,?,?,?,?,?,?,?)");
      preparedStatement.setInt(1, 5);
      preparedStatement.setString(2, titre);
      preparedStatement.setString(3, description);
      preparedStatement.setDate(4, getCurrentDate());
      preparedStatement.setString(5, adresse);
      preparedStatement.setString(6, categorie);
      preparedStatement.setInt(7, 7);
      preparedStatement.setInt(8, 10);     
      preparedStatement.setString(9, UrlPhotoEvenement);
      preparedStatement.setString(10, duree);
      preparedStatement.setInt(11, 5);
      preparedStatement.setInt(12, 1);
      preparedStatement.executeUpdate();
           
    } catch (Exception e) {
      throw e;
    } finally {
      close();
    }

  }
  
  public ResultSet readDataBase() throws Exception{
	  
	  try {
	      
	      Class.forName("com.mysql.jdbc.Driver");	     
	      connect = DriverManager.getConnection( url, utilisateur, motDePasse );
	      statement = connect.createStatement();	     
	      resultSet = statement.executeQuery("select * from SMV.User order by id desc" );
	  } catch (Exception e) {
	      throw e;
	    } finally {
	      //close();
	    }
	  return resultSet;
	  }
	  

 
  private void close() {
    try {
      if (resultSet != null) {
        resultSet.close();
      }

      if (statement != null) {
        statement.close();
      }

      if (connect != null) {
        connect.close();
      }
    } catch (Exception e) {

    }
  }
  
  private static java.sql.Date getCurrentDate() {
	    java.util.Date today = new java.util.Date();
	    return new java.sql.Date(today.getTime());
	}
} 
